package com.st.mall.order.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.st.mall.address.model.AddressModel;
import com.st.mall.address.service.IAddressService;
import com.st.mall.order.model.OrderInfoModel;
import com.st.mall.order.service.IOrderInfoService;
import com.st.mall.user.model.UserModel;

@Controller
@RequestMapping("/orderinfo")
public class OrderInfoController {

	@Autowired
	private IOrderInfoService service;
	@Autowired
	private IAddressService addrService;
	
	@RequestMapping("/insert")
	@ResponseBody
	public int add(OrderInfoModel orderInfoModel) {	
		System.out.println(orderInfoModel.toString());
		return service.insert(orderInfoModel);
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public int delete(String code) {
		return service.delete(code);
	}
	
	@RequestMapping(value = "/selectModel",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String selectModel(OrderInfoModel orderInfoModel){
		List<OrderInfoModel> list = service.selectModel(orderInfoModel);
		int result = service.selectCount(orderInfoModel);
		Map<String,Object> map = new HashMap<>();
		map.put("data",list);
		map.put("count",result);
		return new JSONObject(map).toString();
	}
	
	@RequestMapping(value = "/get",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String get(String code) {
		OrderInfoModel result = service.selectId(code);
		return new JSONObject(result).toString();
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public int update(OrderInfoModel orderInfoModel) {
		int result = service.update(orderInfoModel);
		return result;
	}
	
	@RequestMapping("/jiesuan")
	@ResponseBody
	public String settlement(String proCode,HttpSession session) {
		UserModel user = (UserModel) session.getAttribute("user");
		String custCode = user.getCode();
		
		AddressModel addrModel =new AddressModel();
		addrModel.setUserCode(custCode);
		AddressModel model = addrService.selectId(addrModel);
		if(model==null) 
			return "A";
		String addr = model.getAddr1Name()+"-"+model.getAddr2Name()+"-"+model.getAddr3Name()+"-"+model.getAddr4();	
		OrderInfoModel orderInfoModel = new OrderInfoModel();
		orderInfoModel.setAddrCode(addr);
		orderInfoModel.setCustCode(custCode);
		String result = service.add(orderInfoModel);
		return result;
	}
	
	@RequestMapping(value = "/getAll",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAll(HttpSession session) {
		UserModel user = (UserModel) session.getAttribute("user");
		String custCode = user.getCode();
		OrderInfoModel orderInfoModel = new OrderInfoModel();
		orderInfoModel.setCustCode(custCode);
		List<OrderInfoModel> list = service.selectAll(orderInfoModel);
		return new JSONArray(list).toString();
	}
	
}
